/**
 * @license
 * Copyright 2021 Du Tian Wei
 * SPDX-License-Identifier: Apache-2.0
 */

import * as obvm from '../../runtime/vm.js'
import * as obcanvaslib from './canvas.js'


/**
 * @type {HTMLCanvasElement}
 */
let stage = document.getElementById("stage");

stage.height = parseInt(window.getComputedStyle(stage).height);
stage.width = parseInt(window.getComputedStyle(stage).width);
let obcanvas = new obcanvaslib.OBCanvas2D(stage);
let nativeLibs = [obcanvas.install.bind(obcanvas)];
var scriptArrayBuffer;
var phaserSceneJSONStr;
let loadedScript;
let vm;
let fsm;
function ob_event(name) {
    if (vm) {
        vm.BroadcastMessage(new obvm.OBEventMessage(name, null, null, this));
    }
}
stage.addEventListener('touchstart', () => { ob_event('touchstart') }, false);
stage.addEventListener('mousedown', () => { ob_event('touchstart') }, false);
stage.addEventListener('touchmove', () => { ob_event('touchmove') }, false);
stage.addEventListener('touchcancel', () => { ob_event('touchcancel') }, false);
stage.addEventListener('touchend', () => { ob_event('touchend') }, false);
stage.addEventListener('mouseup', () => { ob_event('touchend') }, false);
stage.addEventListener('click', () => { ob_event('click') }, false);
stage.addEventListener('longpress', () => { ob_event('longpress') }, false);
stage.addEventListener('swipe', () => { ob_event('swipe') }, false);

const inputElement = document.getElementById("input_script");
inputElement.addEventListener("change", () => {
    const fileList = inputElement.files;
    if (fileList.length == 0) {
        return;
    }
    let reader = new FileReader();
    reader.onload = (evt) => {
        stage.width = stage.width;
        scriptArrayBuffer = reader.result;
        loadedScript = obvm.OBScriptLoader.loadScript(scriptArrayBuffer, nativeLibs);
    };
    reader.readAsArrayBuffer(fileList[0]);
}, false);

const runButton = document.getElementById('button_run')
runButton.onclick = function () {
    if (vm) {
        vm = null;
    }
    if (loadedScript) {
        vm = new obvm.OBVM(loadedScript);
        // vm.Output = alert.bind(window);
        let fsmname = document.getElementById("input_start_fsm").value;
        fsm = vm.CreateFSM(fsmname);
        if (!fsm) {
            throw Error("No FSM named " + fsmname);
        }
    }
};

function step() {
    window.requestAnimationFrame(step);
    if (vm) {
        vm.update();
    }
}
window.requestAnimationFrame(step);

if (window.parent) {
    let messageHandler = {
        runProject(evt) {
            stage.width = stage.width;
            scriptArrayBuffer = evt.data.bytes;
            loadedScript = obvm.OBScriptLoader.loadScript(scriptArrayBuffer, nativeLibs);
            vm = new obvm.OBVM(loadedScript);
            // vm.Output = alert.bind(window);
            let fsmname = evt.data.fsm || document.getElementById("input_start_fsm").value;
            fsm = vm.CreateFSM(fsmname);
            if (!fsm) {
                throw Error("No FSM named " + fsmname);
            }
        }
    };
    window.addEventListener("message", receiveMessage);

    function receiveMessage(event) {
        let cmd = event.data.cmd;
        if (messageHandler[cmd]) {
            messageHandler[cmd](event);
        }
    }
}